//
//  SignViewController.m
//  GYMWinners
//
//  Created by Logic Solutions on 3/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SignViewController.h"
#import "FindGymViewController.h"
#import "SignCheckoutViewController.h"

#import "FormatHelper.h"
#import "Utils.h"

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h"
#import "MBProgressHUD.h"
#import "BaseroutAPI.h"
#import "AppDelegate.h"

static double current_latitude = 0.0;
static double current_longitude = 0.0;

@implementation SignViewController
@synthesize locationManager;

-(IBAction)checkInNow{
    
    BOOL canCheckNow=NO;
    
    //1. find position(X,Y) by GPS
    //2. checked (X,Y) by server, and return result
    
    if (canCheckNow) 
    {
        
        SignCheckoutViewController* signCheckoutViewController=[[SignCheckoutViewController alloc] init];
        [[self navigationController]pushViewController:signCheckoutViewController animated:NO];
        
    }
    else
    { 
        //alert with options
        
//        UIAlertView* alert=[[UIAlertView alloc]
//                            initWithTitle:@"未知的健身中心" 
//                            message:@"添加健身中心或者重试. 本次健身时间会在健身中心得到验证后生效." 
//                            delegate:self 
//                            cancelButtonTitle:@"取消"
//                            otherButtonTitles:@"重试",@"添加健身中心", nil];
//        [alert show];       
    
        // asking login
        NSMutableDictionary *slist = [[NSMutableDictionary alloc] init];
        [slist setObject:[[BaseroutAPI sharedInstance] zGetVarEx:@"customerid"] forKey:@"cid"];
        [slist setObject:[NSString stringWithFormat:@"%g", current_latitude] forKey:@"lat"];
        [slist setObject:[NSString stringWithFormat:@"%g", current_longitude] forKey:@"lon"];
        [self zSendServer:slist theTitle:@"请稍等" theCommand:@"/checkin/start"]; 
    
    }
    
}

-(void)findGym
{
    FindGymViewController* findGymViewController=[[FindGymViewController alloc] init];
    [[self navigationController] pushViewController:findGymViewController animated:YES];
}   


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=100.0f;
    [locationManager startUpdatingLocation];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)dealloc {
    NSLog(@"dealloc sign view");
    locationManager.delegate = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -- UIAlertViewDelegate --

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
    
    NSLog(@"clickedButtonAtIndex:%d",buttonIndex); 
    
    if (buttonIndex==1) 
    { 
        //try again
        [self checkInNow];
        
    }else if(buttonIndex==2)
    {
        [self findGym];
    }
        
}  

// called when phone location was changed
- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{
    
    NSString* lat=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.latitude];
    NSString* lng=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.longitude];
    NSString* acc=[[NSString alloc] initWithFormat:@"%g",newLocation.horizontalAccuracy];
    NSLog(@"%@__%@__%@",lat,lng,acc);
    
    current_latitude = newLocation.coordinate.latitude;
    current_longitude = newLocation.coordinate.longitude;
    
}

#pragma mark - ASIFormDataRequest delegate

- (void)requestFinished:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (request.responseStatusCode == 200) {
        
        NSString *responseString = [request responseString];
        NSLog(@"responseString=[%@]", responseString);   
        //NSArray *responseArr = [responseString JSONValue];
        
        
        SignCheckoutViewController* signCheckoutViewController=[[SignCheckoutViewController alloc] init];
        [[self navigationController]pushViewController:signCheckoutViewController animated:NO];
        
        
    } else {
        NSLog(@"Unexpected error");  
        [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
    }
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    NSError *error = [request error];
    NSLog(@"Error=%@", error.localizedDescription);   
    
    [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
}

@end
